﻿#ifndef CImgDownloadTh_H
#define CImgDownloadTh_H
/********************************************************************************
** @ Date:2024/12/13
** @ Author: HanJiaXin
** @ Description:图片下载类
** @ Version:1.0.0
** @ ClassName:
** @ Fun:
********************************************************************************/
#include <QObject>
#include <QMutex>
#include <QWaitCondition>
#include <QQueue>
#include <QNetworkAccessManager>
#include <QLabel>
#include <QThread>

class CImgDownloadTh : public QThread
{
	Q_OBJECT

public:
	static CImgDownloadTh *instance();  // 获取单例

	void addRequest(const QUrl &url, QLabel *label, QString sPageUuid);//添加单个Http请求
	void addRequest(QQueue<QPair<QUrl, QLabel*>> pairRequest, QString sPageUuid);//批量添加http请求,上层不用手动清空队列

	void clearRequest();

protected:
	void run() override;

signals:
	void imageDownloaded(QLabel* pLabel,QPixmap pixmap,QString sPageUuid);

private:
	explicit CImgDownloadTh(QObject *parent = nullptr);
	~CImgDownloadTh();

	void downloadImage(const QUrl &url, QLabel *label);

	QQueue<QPair<QUrl, QLabel*>> m_requestQueue;  // 请求队列
	QMutex m_queueMutex;  // 保护队列的互斥量
	QWaitCondition m_condition;  // 用于线程同步

	QNetworkAccessManager *m_networkManager;  // 网络管理器
	QString m_sPageUuid;
	QString m_sOldPageUuid;
	bool m_bIsFrist;
};

#endif